/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InterfazCrearUSuario.java
 *
 * Created on 18/10/2011, 01:07:17 PM
 */
package presentacion;

import dominio.Profesor;
import Paneles.PanelGeneral;
import business.AdministradorBo;
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import business.EstudianteBO;
import business.ProfesorBo;
import business.UsuarioBO;
import dominio.Estudiante;
import dominio.Usuario;
import java.util.Collection;
import java.util.Iterator;
import javax.swing.JOptionPane;

/**
 *
 * @author SEBASTIAN
 */
public class InterfazCrearUSuario extends javax.swing.JFrame {

    private ImageIcon icono;
    private Long indiceDocEst[];

    /** Creates new form InterfazCrearUSuario */
    public InterfazCrearUSuario() {
        initComponents();
        PanelGeneral panel = new PanelGeneral();
        this.add(panel, BorderLayout.EAST);
        icono = new ImageIcon(getClass().getResource("/Imagenes/icon.gif"));
        this.setIconImage(icono.getImage());
        jLabel6.setVisible(false);
        jLabel7.setVisible(false);
        jLabel8.setVisible(false);
        jTextField1.setVisible(false);
        jTextField2.setVisible(false);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jTextField3 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel5 = new javax.swing.JLabel();
        jPasswordField2 = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Crear Usuario");
        setName("Crear Usuario"); // NOI18N

        jLabel3.setText("Nick:");

        jLabel4.setText("Contraseña:");

        jLabel5.setText("Confirmar Contraseña:");

        jLabel1.setText("Tipo de usuario:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Escoja:", "Estudiante", "Profesor", "Administrador" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("Crear");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Asignar a:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccione" }));

        jLabel6.setText("Nombre");

        jLabel7.setText("Apellido");

        jLabel8.setText("El administrador será creado por eso debe registrar los datos");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(124, 124, 124)
                        .addComponent(jComboBox1, 0, 178, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel2)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordField1, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(jPasswordField2, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(jComboBox2, 0, 178, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel8)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jPasswordField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (String.valueOf(jPasswordField1.getPassword()).equals(String.valueOf(jPasswordField2.getPassword()))) {
            if (jComboBox1.getSelectedIndex() == 1) {//crear usuario a estudiante
                if (jComboBox2.getItemCount() == 1) {
                    JOptionPane.showMessageDialog(this, "Seleccione un estudiante", "", JOptionPane.INFORMATION_MESSAGE);
                }
                else {
                    asignarUsuarioAEstudiante();
                }
            }
            if (jComboBox1.getSelectedIndex() == 2) {//crear usuario a prf
                if (jComboBox2.getItemCount() == 1) {
                    JOptionPane.showMessageDialog(this, "Seleccione un profesor", "Profesores sin usuario: 0", JOptionPane.INFORMATION_MESSAGE);
                }
                else {
                    asignarUsuarioAProfesor();
                }
            }
            if (jComboBox1.getSelectedIndex() == 3) {//crear usuario a ad
                    asignarUsuarioAAdministrador();
            }
        }
        else {
            JOptionPane.showMessageDialog(this, "Claves no concordantes");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed

        if (jComboBox1.getSelectedIndex() == 1) {
            jLabel6.setVisible(false);
            jLabel7.setVisible(false);
            jLabel8.setVisible(false);
            jTextField1.setVisible(false);
            jTextField2.setVisible(false);
            poblarComboEstudiantes();
            if (jComboBox2.getItemCount() == 1) {
                JOptionPane.showMessageDialog(this, "Estudiantes sin usuario: 0", "No hay estudiantes sin usuario", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if (jComboBox1.getSelectedIndex() == 2) {
            jLabel6.setVisible(false);
            jLabel7.setVisible(false);
            jLabel8.setVisible(false);
            jTextField1.setVisible(false);
            jTextField2.setVisible(false);
            poblarComboProfesores();
            if (jComboBox2.getItemCount() == 1) {
                JOptionPane.showMessageDialog(this, "Profesores sin usuario: 0", "No hay Profesores sin usuario", JOptionPane.INFORMATION_MESSAGE);
            }
        }
        if (jComboBox1.getSelectedIndex() == 3) {
            poblarComboAdministradores();
        }
    }//GEN-LAST:event_jComboBox1ActionPerformed
    private void poblarComboEstudiantes() {
        EstudianteBO ebo = new EstudianteBO();
        Collection<Estudiante> e = ebo.encontrarEstudiantesSinUsuario();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Seleccione"}));
        Iterator<Estudiante> itp = e.iterator();
        if (e.size() > 0) {
            indiceDocEst = new Long[e.size()];
        }
        int i = 0;
        while (itp.hasNext()) {
            Estudiante p = itp.next();
            indiceDocEst[i] = p.getDocEstudiante();
            jComboBox2.addItem(p.getApellidoEst() + " " + p.getNombreEst() + " [Doc: " + p.getDocEstudiante() + "]");
            i++;
        }
    }

    private void asignarUsuarioAEstudiante() {
        UsuarioBO ubo = new UsuarioBO();
        Usuario u = ubo.crearUsuario(jTextField3.getText(), String.valueOf(jPasswordField1.getPassword()), "Estudiante");
        EstudianteBO ebo = new EstudianteBO();
        ebo.asignarUsuarioAEstudiante(indiceDocEst[jComboBox2.getSelectedIndex() - 1], jTextField3.getText());
        poblarComboEstudiantes();
        operacionFinal();
    }

    private void poblarComboAdministradores() {
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"No Aplica"}));
        jComboBox2.setEnabled(false);
        jLabel6.setVisible(true);
        jLabel7.setVisible(true);
        jLabel8.setVisible(true);
        jTextField1.setVisible(true);
        jTextField2.setVisible(true);
    }

    private void asignarUsuarioAProfesor() {
        UsuarioBO ubo = new UsuarioBO();
        Usuario u = ubo.crearUsuario(jTextField3.getText(), String.valueOf(jPasswordField1.getPassword()), "Profesor");
        ProfesorBo ebo = new ProfesorBo();
        ebo.asignarUsuarioAProfesor(indiceDocEst[jComboBox2.getSelectedIndex() - 1], jTextField3.getText());
        poblarComboProfesores();
        operacionFinal();
    }

    private void poblarComboProfesores() {
        ProfesorBo pbo = new ProfesorBo();
        Collection<Profesor> p = pbo.encontrarProfesoresSinUsuario();
        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Seleccione"}));
        Iterator<Profesor> itp = p.iterator();
        if (p.size() > 0) {
            indiceDocEst = new Long[p.size()];
        }
        int i = 0;
        while (itp.hasNext()) {
            Profesor profeActual = itp.next();
            indiceDocEst[i] = profeActual.getDocProfesor();
            jComboBox2.addItem(profeActual.getApellidoProfesor() + " " + profeActual.getNombreProfesor() + " [Doc: " + profeActual.getDocProfesor() + "]");
            i++;
        }
    }

    private void asignarUsuarioAAdministrador() {
        UsuarioBO ubo = new UsuarioBO();
        Usuario u = ubo.crearUsuario(jTextField3.getText(), String.valueOf(jPasswordField1.getPassword()), "Admin");
        AdministradorBo ebo = new AdministradorBo();
        ebo.crearAdministrador(jTextField1.getText(), jTextField2.getText(), jTextField3.getText());
        operacionFinal();
    }

    public void operacionFinal() {
        JOptionPane.showMessageDialog(this, "El usuario se asignado satisfactoriamente", "Operacion Existosa", JOptionPane.INFORMATION_MESSAGE);
        jTextField3.setText("");
        jPasswordField1.setText("");
        jPasswordField2.setText("");
        jTextField1.setText("");
        jTextField2.setText("");

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new InterfazCrearUSuario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JPasswordField jPasswordField2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
}
